import 'package:flutter/material.dart';

import 'dialog.dart';
import 'dismiss_wrap.dart';
import 'sliver_example.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      // home: MyHomePage(title: 'Flutter Demo Home Page'),
      home: SliverExample(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  ScrollController _ctl;

  @override
  void initState() {
    super.initState();
    _ctl = ScrollController();
  }

  void _incrementCounter() {
    showMyDialog(
      context,
      MyDialog(
        ctl: _ctl,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: RefreshIndicator(
        onRefresh: () async {
          await Future.delayed(Duration(seconds: 2));
        },
        child: ListView.builder(
          physics: ClampingScrollPhysics(),
          itemBuilder: (c, i) => ListTile(
            title: Text(i.toString()),
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class MyDialog extends StatefulWidget {
  final ScrollController ctl;

  const MyDialog({
    Key key,
    this.ctl,
  }) : super(key: key);

  @override
  _MyDialogState createState() => _MyDialogState();
}

class _MyDialogState extends State<MyDialog> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 300,
      child: Material(
        color: Colors.transparent,
        child: DismissWrapper(
          ctl: widget.ctl,
          children: <Widget>[
            Container(
              height: 300,
              color: Colors.white,
              child: MediaQuery.removePadding(
                removeTop: true,
                context: context,
                child: ListView.builder(
                  physics: ClampingScrollPhysics(),
                  controller: widget.ctl,
                  itemBuilder: (BuildContext context, int index) {
                    return Container(
                      height: 40,
                      child: ListTile(title: Text(index.toString())),
                    );
                  },
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
